<?php
/**
 * Author: zsx
 * Date: 2022/9/14
 * Time: 12:17
 * 每个月1号 晚上00:00分执行一次
 * 整合展厅表数据
 */
date_default_timezone_set('PRC');
// 链接数据
$conn = mysqli_connect('localhost', 'root', 'root', 'huiyi');
$month = date('Y-m-01', strtotime('-1 month'));
if ($conn) {
  $data = [];
  $reData = [];
  $sql = "select * from show_t where states=1";
  $result = mysqli_query($conn, $sql);
  $sql1 = "insert into show_statistics(productn,model,date,amoumt,cost) VALUES ";

  while($row = mysqli_fetch_assoc($result))
  {
    $reData[] = $row;
  }
  // 根据sku
  if ($reData) {
    foreach ($reData as $value) {
      $str = $value['productn'].'-'.$value['model'].'-'.$value['cost'];
      if (isset($data[$str])) {
        $data[$str]['amoumt'] += $value['amoumt'];
      } else {
        $data[$str] = [
          'productn' => $value['productn'],
          'model' => $value['model'],
          'cost' => $value['cost'],
          'amoumt' => $value['amoumt']
        ];
      }
    }
  }
  foreach ($data as $v) {
    $sql1 .= "('{$v['productn']}', '{$v['model']}', '{$month}', {$v['amoumt']},{$v['cost']} ),";
  }
  if ($sql1 && $data) {
    $sql1 = substr($sql1, 0, strlen($sql1)-1).';';
    mysqli_query($conn, $sql1);
  }
  mysqli_close($conn);
}
